/*
 * Copyright 2013 Pavel Stastny <pavel.stastny at gmail.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aplikator.client.local.widgets;

import com.github.gwtbootstrap.client.ui.Form;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import org.aplikator.client.shared.data.Record;
import org.aplikator.client.shared.descriptor.FormDTO;
import org.aplikator.client.shared.descriptor.PropertyDTO;
import org.aplikator.client.shared.descriptor.WizardPageDTO;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Pavel Stastny <pavel.stastny at gmail.com>
 */
public class WizardFormWidget extends AbstractFormContainerWidget implements ValueChangeHandler, HasFields {

    private List<DataField<?>> fields = new ArrayList<DataField<?>>();
    private WizardPageDTO view;
    private Record originalRecord;
    
    
    
    public WizardFormWidget(WizardPageDTO view, FormDTO formDTO) {
        this.view = view;
        Form wrapper = new Form();
        FormDTO descriptor = formDTO;
        
        super.initializeForm(descriptor, wrapper);
    }
    

    @SuppressWarnings("unchecked")
    public void registerDataField(DataField<? extends Object> field) {
//        for (DataField<?> df : fields) {
//            if (df.getProperty().getId().equals(field.getProperty().getId()))
//                throw new IllegalArgumentException("property "+field.getProperty().getId()+" already defined");
//        }
        fields.add(field);
        field.addValueChangeHandler(this);
    }

    @Override
    public void registerNestedCollection(NestedCollectionWidget collection) {
        throw new UnsupportedOperationException("registerNestedCollection not supported");
    }

    @Override
    public void registerBinaryField(BinaryFieldWidget binaryField) {
        throw new UnsupportedOperationException("registerBinaryField not supported");
    }

    @Override
    public void registerFunctionButton(FunctionButtonWidget functionButton) {
        throw new UnsupportedOperationException("registerFunctionButton not supported");
    }

    @SuppressWarnings("unchecked")
    public Record getWizardRecord() {
        Record record = new Record();
        for (DataField field : fields) {
            PropertyDTO prop = field.getProperty();
            Serializable val = field.getValue();
            prop.setValue(record, val);
        }
        return record;
    }

    @SuppressWarnings({ "unchecked" })
    public Record addFieldsToExistingRecord(Record record) {
        for (DataField field : fields) {
            PropertyDTO prop = field.getProperty();
            Serializable val = field.getValue();
            prop.setValue(record, val);
        }
        return record;
    }

    @Override
    public Record getPopulateRecord() {
        return this.originalRecord;
    }
            
    
    
    @Override
    public void setDirty(boolean dirty){

    }
    
    public void setOriginal(Record rec) {
        this.originalRecord = rec;
    }
    
    @Override
    public Record getOriginal(){
        return this.originalRecord;
    }

    @Override
    public List<DataField<?>> getDataFields() {
        return this.fields;
    }

    
    @SuppressWarnings({ "unchecked" })
    public void onValueChange(ValueChangeEvent event) {
        //setDirty(true);
        /*
        if (event.getSource().getClass().equals(ReferenceFieldWidget.class)) {
            ReferenceFieldWidget source = (ReferenceFieldWidget) event.getSource();
            Record reference = source.getSelectedRecord();
            if (reference != null) {
                for (DataField field : fields) {
                    PropertyDTO prop = field.getProperty();
                    if (prop.getRefferedThrough() != null) {// TODO kontrolovat
                                                            // pres co je
                                                            // referovano...
                        PropertyDTO unreferencedProperty = prop.cloneUnreferenced();
                        Serializable val = unreferencedProperty.getValue(reference);
                        if (val != null) {
                            field.setValue(val);
                        }
                    }
                }
            }
        }
        */
    }
}
